﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
using TravelLink.DAL;

namespace TravelLink.WebApp.UserControl.Member.JobEditable {
    public class JobEditableFacade {


        Dictionary<string, ASPxGridView> _gridViewDict;

        static Dictionary<string, JobEditableBase> CnJobProviders;

        static Dictionary<string, JobEditableBase> EnJobProviders;

        static JobEditableFacade() {
            EnJobProviders = new Dictionary<string, JobEditableBase>(7);
            CnJobProviders = new Dictionary<string, JobEditableBase>(7);


            CnJobProviders.Add("aspGridEducation", new JobEditableEducation(false));
            EnJobProviders.Add("aspGridEducation", new JobEditableEducation(true));


            CnJobProviders.Add("aspGridSkills", new JobEditableSkill(false));
            EnJobProviders.Add("aspGridSkills", new JobEditableSkill(true));


            CnJobProviders.Add("aspxGridMembership", new JobEditableMembership(false));
            EnJobProviders.Add("aspxGridMembership", new JobEditableMembership(true));


            CnJobProviders.Add("aspxGridAward", new JobEditableAward(false));
            EnJobProviders.Add("aspxGridAward", new JobEditableAward(true));


            CnJobProviders.Add("aspxGridQualification", new JobEditableQualification(false));
            EnJobProviders.Add("aspxGridQualification", new JobEditableQualification(true));


            CnJobProviders.Add("aspxGridCareer", new JobEditableCarrer(false));
            EnJobProviders.Add("aspxGridCareer", new JobEditableCarrer(true));






        }

        private bool _isEn = false;
        private int _resumeId = 0;
        private string _userCode = Guid.Empty.ToString();

        private ResumeParam _p;
        public JobEditableFacade(Dictionary<string, ASPxGridView> gridViewDict,
            bool isEn,
            int resumeId,
            string userCode) {
            _gridViewDict = gridViewDict;
            _isEn = isEn;
            _userCode = userCode;
            _resumeId = resumeId;
            _p = new ResumeParam();
            _p.ResumeIdx = resumeId;
            _p.UserCode = _userCode;
        }



        public void Initialize() {
            JobEditableBase temp = null;
            foreach (var gridView in _gridViewDict.Values) {
                temp = GetProviders(gridView.ID);
                if (temp != null) {
                    temp.Initialize(CheckIsEding, BindAction, _p);
                }
            }
        }


        public void ClearSession() {
            JobEditableBase temp = null;
            foreach (var gridView in _gridViewDict.Values) {
                temp = GetProviders(gridView.ID);
                if (temp != null) {
                    temp.Clear(gridView.ID);
                }
            }

        }


        public void RowDeleting(object sender, ASPxDataDeletingEventArgs e) {
            ASPxGridView grid = sender as ASPxGridView;
            if (grid == null) return;
            var provider = GetProviders(grid.ID);
            if (provider != null) provider.RowDeleting(BindAction, _p, e, grid);

        }


        public void RowUpdating(object sender, ASPxDataUpdatingEventArgs e) {
            ASPxGridView grid = sender as ASPxGridView;
            if (grid == null) return;
            var provider = GetProviders(grid.ID);
            if (provider != null) provider.RowUpdating(BindAction, _p, e, grid);

        }

        public void RowInserting(object sender, ASPxDataInsertingEventArgs e) {
            ASPxGridView grid = sender as ASPxGridView;
            if (grid == null) return;
            var provider = GetProviders(grid.ID);
            if (provider != null) provider.RowInserting(BindAction, _p, e, grid);

        }

        public void Save(TL_Resume resume) {
            JobEditableBase temp = null;
            foreach (var gridView in _gridViewDict.Values) {
                temp = GetProviders(gridView.ID);
                if (temp != null) {
                    temp.RowSave(_p, BindAction);
                }
            }
        }

        #region Delegate Implements

        protected void BindAction(string aspGridName, object source) {
            ASPxGridView grid = null;
            if (!_gridViewDict.TryGetValue(aspGridName, out grid)) {
                return;
            }

            grid.DataSource = source;
            grid.DataBind();
        }

        protected bool CheckIsEding(string aspGridName) {
            ASPxGridView grid = null;
            if (!_gridViewDict.TryGetValue(aspGridName, out grid)) {
                return false;
            }
            return grid.IsNewRowEditing || grid.IsEditing;
        }
        #endregion

        private JobEditableBase GetProviders(string name) {
            JobEditableBase provider = null;
            if (_isEn) {
                EnJobProviders.TryGetValue(name, out provider);
            }
            else {
                CnJobProviders.TryGetValue(name, out provider);
            }
            return provider;
        }



    }
}